﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;

namespace Micua.Core.IoC.Unity
{

    /// <summary>
    /// 自定义的控制器工厂，使MVC控制器可以支持依赖注入
    /// </summary>
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer _container;
        /// <summary>
        /// 构造函数
        /// </summary>
        public UnityControllerFactory()
        {
            _container = UnityContext.Instance.Container;
        }    
        /// <summary>
        /// 检索指定请求上下文和控制器类型的控制器实例。
        /// </summary>
        /// <param name="requestContext">HTTP 请求的上下文，其中包括 HTTP 上下文和路由数据。</param>
        /// <param name="controllerType">控制器的类型。</param>
        /// <exception cref="System.Web.HttpException">controllerType 为 null。</exception>
        /// <exception cref="System.ArgumentException">无法分配 controllerType。</exception>
        /// <exception cref="System.InvalidOperationException">无法创建 controllerType 的实例。</exception>
        /// <returns>控制器实例。</returns>
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (requestContext == null)
            {
                //return null;
                //throw new ArgumentNullException("requestContext");
                throw new System.Web.HttpException(0x194, "Not Found");
            }
            if (controllerType == null)
            {
                //requestContext.HttpContext.Response.Write("你要找到找不到！");
                //return null;
                //return base.GetControllerInstance(requestContext, controllerType);
                
                throw new System.Web.HttpException(0x194,"Not Found");
            }

            var controller = _container.Resolve(controllerType) as IController;

            return controller;
        }
    }
}